package com.rf.richfitwheel.outbill.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.outbill.service.AcceptBillInfoService;
import com.rf.richfitwheel.outbill.service.OutBillInfoService;
import com.rf.richfitwheel.outbill.service.ReceiptBillInfoService;
import com.rf.richfitwheel.workflow.conf.WorkflowContants;
import com.rf.richfitwheel.workflow.entity.WorkflowMsgVo;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class WorkflowReceiver {
    @Autowired
    private OutBillInfoService outBillInfoService;

    @Autowired
    private AcceptBillInfoService acceptBillInfoService;

    @Autowired
    private ReceiptBillInfoService receiptBillInfoService;

    @RabbitListener(queuesToDeclare = @Queue("workflow.bill"))
    @RabbitHandler
    public void processWorkflowMsg(WorkflowMsgVo workflowMsgVo, Channel channel, @Headers Map<String, Object> headers) throws IOException {
        System.out.println(Thread.currentThread().getName() + " 接收到来自队列的消息：开始========");
        System.out.println(workflowMsgVo.toString());
        System.out.println(Thread.currentThread().getName() + " 接收到来自队列的消息：结束========");
        long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            Map<String, Object> variables = workflowMsgVo.getVariables();
            String processKey = (String) variables.get("processKey");//流程编码
            String approveNo = (String) variables.get("approveNo");//审批编号
            String orgCode = (String) variables.get("orgCode");//企业编码
            String processCode = (String) variables.get("processCode");//流程代码
            String businessKey = (String) variables.get("businessKey");//业务主键
            String processInstanceId = (String) variables.get("processInstanceId");

            /**
             *            下面判断是根据  队列通知事件名称 来做流程任务的判断
             *     public final static String EVENTNAME_PROCESS_START = "processStart";//流程开始
             *     public final static String EVENTNAME_PROCESS_END = "processEnd";//流程正常结束
             *     public final static String EVENTNAME_PROCESS_TERMINATED = "processTerminated";//流程被终止
             *     public final static String EVENTNAME_TASK_CREATE = "taskCreate";//任务生成
             */

            /**
             * 1. 流程开始
             */
            if (workflowMsgVo.getEventName().equals(WorkflowContants.EVENTNAME_PROCESS_START)) {
                if (StringUtils.isNotBlank(processCode)) {
                    switch (processCode) {
                        //出票登记
                        case WorkflowModelCode.OUT_BILL_BILL101:
                            outBillInfoService.updateOutBill(businessKey, WorkflowContants.PROCESS_STATUS_1, workflowMsgVo.getProcessInstanceId(), variables);
                            break;
                        //提示承兑
                        case WorkflowModelCode.OUT_BILL_BILL102:
                            acceptBillInfoService.updateAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_1, workflowMsgVo.getProcessInstanceId());
                            break;
                        //提示出票
                        case WorkflowModelCode.OUT_BILL_BILL103:
                            receiptBillInfoService.updateReceiptBill(businessKey, WorkflowContants.PROCESS_STATUS_1, workflowMsgVo.getProcessInstanceId());
                            break;
                        //撤销提示承兑
                        case WorkflowModelCode.OUT_BILL_BILL104:
                            acceptBillInfoService.backAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_1, workflowMsgVo.getProcessInstanceId());
                            break;
                        //撤銷提示出票
                        case WorkflowModelCode.OUT_BILL_BILL105:
                            receiptBillInfoService.backAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_1, workflowMsgVo.getProcessInstanceId());
                            break;
                        //撤銷出票登记
                        case WorkflowModelCode.OUT_BILL_BILL106:
                            outBillInfoService.backAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_1, workflowMsgVo.getProcessInstanceId());
                            break;
                        default:
                            break;
                    }
                } else {
                    switch (processKey) {//自定义流程
                        case ""://
                        default:
                            break;
                    }
                }
            }
//            2.流程正常结束
            if (workflowMsgVo.getEventName().equals(WorkflowContants.EVENTNAME_PROCESS_END)) {
                if (StringUtils.isNotBlank(processCode)) {
                    switch (processCode) {
                        //出票登记提交
                        case WorkflowModelCode.OUT_BILL_BILL101:
                            outBillInfoService.updateOutBill(businessKey, WorkflowContants.PROCESS_STATUS_2, workflowMsgVo.getProcessInstanceId(), variables);
                            break;
                        case WorkflowModelCode.OUT_BILL_BILL102:
                            acceptBillInfoService.updateAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_2, workflowMsgVo.getProcessInstanceId());
                            break;
                        //提示出票
                        case WorkflowModelCode.OUT_BILL_BILL103:
                            receiptBillInfoService.updateReceiptBill(businessKey, WorkflowContants.PROCESS_STATUS_2, workflowMsgVo.getProcessInstanceId());
                            break;
                        case WorkflowModelCode.OUT_BILL_BILL104:
                            acceptBillInfoService.backAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_2, workflowMsgVo.getProcessInstanceId());
                            break;
                        case WorkflowModelCode.OUT_BILL_BILL105:
                            receiptBillInfoService.backAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_2, workflowMsgVo.getProcessInstanceId());
                            break;
                        //撤銷出票登记
                        case WorkflowModelCode.OUT_BILL_BILL106:
                            outBillInfoService.backAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_2, workflowMsgVo.getProcessInstanceId());
                            break;
                        default:
                            break;
                    }
                } else {
                    switch (processKey) {//自定义流程
                        case ""://
                        default:
                            break;
                    }
                }
            }
//            3.流程被终止8332.78
            if (workflowMsgVo.getEventName().equals(WorkflowContants.EVENTNAME_PROCESS_TERMINATED)) {
                if (StringUtils.isNotBlank(processCode)) {
                    switch (processCode) {
                        //出票登记提交
                        case WorkflowModelCode.OUT_BILL_BILL101:
                            outBillInfoService.updateOutBill(businessKey, WorkflowContants.PROCESS_STATUS_3, workflowMsgVo.getProcessInstanceId(), variables);
                            break;
                        //提示承兑
                        case WorkflowModelCode.OUT_BILL_BILL102:
                            acceptBillInfoService.updateAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_3, workflowMsgVo.getProcessInstanceId());
                            break;
                        //提示出票
                        case WorkflowModelCode.OUT_BILL_BILL103:
                            receiptBillInfoService.updateReceiptBill(businessKey, WorkflowContants.PROCESS_STATUS_3, workflowMsgVo.getProcessInstanceId());
                            break;
                        case WorkflowModelCode.OUT_BILL_BILL104:
                            acceptBillInfoService.backAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_3, workflowMsgVo.getProcessInstanceId());
                            break;
                        //撤銷提示出票
                        case WorkflowModelCode.OUT_BILL_BILL105:
                            receiptBillInfoService.backAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_3, workflowMsgVo.getProcessInstanceId());
                            break;
                        //撤銷出票登记
                        case WorkflowModelCode.OUT_BILL_BILL106:
                            outBillInfoService.backAcceptBill(businessKey, WorkflowContants.PROCESS_STATUS_3, workflowMsgVo.getProcessInstanceId());
                            break;
                        default:
                            break;
                    }
                } else {
                    switch (processKey) {//自定义流程
                        case ""://
                        default:
                            break;
                    }
                }
            }
//            4.流程任务生成
            if (workflowMsgVo.getEventName().equals(WorkflowContants.EVENTNAME_TASK_CREATE)) {

            }


            //手工ack
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            channel.basicReject(deliveryTag, false);
            e.printStackTrace();
        }

    }


    public String findWorkflowStatus(String eventName) {
        switch (eventName) {
            case WorkflowContants.EVENTNAME_PROCESS_START: //流程开始
                return WorkflowContants.PROCESS_STATUS_1;
            case WorkflowContants.EVENTNAME_PROCESS_END:   //流程结束
                return WorkflowContants.PROCESS_STATUS_2;
            case WorkflowContants.EVENTNAME_PROCESS_TERMINATED: //流程被终止
                return WorkflowContants.PROCESS_STATUS_3;
            case WorkflowContants.EVENTNAME_TASK_CREATE:   //当前节点生成审批任务
                return WorkflowContants.PROCESS_STATUS_1;
            default:
                return null;
        }

    }
}